<template>
  <div class="question-container">
    <el-card class="question-card">
      <div slot="header" class="card-header">
        <span>问题数据管理</span>
        <el-button
          type="primary"
          size="small"
          @click="handleAdd">
          添加问题
        </el-button>
      </div>

      <!-- 搜索和过滤 -->
      <div class="filter-container">
        <el-form :inline="true" :model="filterForm" class="filter-form">
          <el-form-item label="问题内容">
            <el-input
              v-model="filterForm.content"
              placeholder="请输入问题内容"
              clearable
              @clear="handleFilter">
            </el-input>
          </el-form-item>
          <el-form-item label="维度">
            <el-select
              v-model="filterForm.dimension"
              placeholder="请选择维度"
              clearable
              @clear="handleFilter">
              <el-option label="外向-内向 (E-I)" value="EI"></el-option>
              <el-option label="感觉-直觉 (S-N)" value="SN"></el-option>
              <el-option label="思考-情感 (T-F)" value="TF"></el-option>
              <el-option label="判断-知觉 (J-P)" value="JP"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="handleFilter">查询</el-button>
            <el-button @click="resetFilter">重置</el-button>
          </el-form-item>
        </el-form>
      </div>

      <!-- 问题列表 -->
      <el-table
        v-loading="loading"
        :data="questions"
        style="width: 100%">
        <el-table-column
          prop="id"
          label="ID"
          width="80">
        </el-table-column>
        <el-table-column
          prop="content"
          label="问题内容"
          min-width="200">
        </el-table-column>
        <el-table-column
          prop="dimension"
          label="维度"
          width="100">
          <template slot-scope="scope">
            <el-tag :type="getDimensionTagType(scope.row.dimension)">
              {{ getDimensionLabel(scope.row.dimension) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column
          label="选项A"
          width="200">
          <template slot-scope="scope">
            <div>{{ scope.row.optionA }}</div>
            <div class="score-info">
              <el-tag size="mini" v-if="scope.row.aescore === 1">E:1</el-tag>
              <el-tag size="mini" v-if="scope.row.aiscore === 1">I:1</el-tag>
              <el-tag size="mini" v-if="scope.row.asscore === 1">S:1</el-tag>
              <el-tag size="mini" v-if="scope.row.anscore === 1">N:1</el-tag>
              <el-tag size="mini" v-if="scope.row.atscore === 1">T:1</el-tag>
              <el-tag size="mini" v-if="scope.row.afscore === 1">F:1</el-tag>
              <el-tag size="mini" v-if="scope.row.ajscore === 1">J:1</el-tag>
              <el-tag size="mini" v-if="scope.row.apscore === 1">P:1</el-tag>
            </div>
          </template>
        </el-table-column>
        <el-table-column
          label="选项B"
          width="200">
          <template slot-scope="scope">
            <div>{{ scope.row.optionB }}</div>
            <div class="score-info">
              <el-tag size="mini" v-if="scope.row.bescore === 1">E:1</el-tag>
              <el-tag size="mini" v-if="scope.row.biscore === 1">I:1</el-tag>
              <el-tag size="mini" v-if="scope.row.bsscore === 1">S:1</el-tag>
              <el-tag size="mini" v-if="scope.row.bnscore === 1">N:1</el-tag>
              <el-tag size="mini" v-if="scope.row.btscore === 1">T:1</el-tag>
              <el-tag size="mini" v-if="scope.row.bfscore === 1">F:1</el-tag>
              <el-tag size="mini" v-if="scope.row.bjscore === 1">J:1</el-tag>
              <el-tag size="mini" v-if="scope.row.bpscore === 1">P:1</el-tag>
            </div>
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          width="150"
          fixed="right">
          <template slot-scope="scope">
            <el-button
              size="mini"
              @click="handleEdit(scope.row)">
              编辑
            </el-button>
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.row)">
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <div class="pagination-container">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[10, 20, 30, 50]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
        </el-pagination>
      </div>

      <!-- 添加/编辑对话框 -->
      <el-dialog
        :title="dialogTitle"
        :visible.sync="dialogVisible"
        width="60%"
        @close="handleDialogClose">
        <el-form
          ref="form"
          :model="form"
          :rules="rules"
          label-width="100px">
          <el-form-item label="问题内容" prop="content">
            <el-input
              type="textarea"
              v-model="form.content"
              :rows="3"
              placeholder="请输入问题内容">
            </el-input>
          </el-form-item>
          <el-form-item label="维度" prop="dimension">
            <el-select 
              v-model="form.dimension" 
              placeholder="请选择维度" 
              style="width: 100%"
              @change="handleDimensionChange">
              <el-option label="外向-内向 (E-I)" value="EI"></el-option>
              <el-option label="感觉-直觉 (S-N)" value="SN"></el-option>
              <el-option label="思考-情感 (T-F)" value="TF"></el-option>
              <el-option label="判断-知觉 (J-P)" value="JP"></el-option>
            </el-select>
          </el-form-item>
          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="选项A" prop="optionA">
                <el-input
                  type="textarea"
                  v-model="form.optionA"
                  :rows="2"
                  placeholder="请输入选项A">
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="选项B" prop="optionB">
                <el-input
                  type="textarea"
                  v-model="form.optionB"
                  :rows="2"
                  placeholder="请输入选项B">
                </el-input>
              </el-form-item>
            </el-col>
          </el-row>

          <!-- 分数设置区域 -->
          <div class="score-settings" v-if="form.dimension">
            <h3>分数设置说明</h3>
            <p class="score-tip">根据问题维度，为选项A和B分别设置对应的分数。每个选项只能设置一个维度的分数为1，其他维度分数默认为0。</p>
            
            <div class="score-section">
              <h4>选项A分数</h4>
              <el-row :gutter="20">
                <template v-if="form.dimension === 'EI'">
                  <el-col :span="12">
                    <el-form-item label="外向(E)">
                      <el-radio-group v-model="form.aescore" @change="val => handleAScoreChange('E', val)">
                        <el-radio :label="1">1分</el-radio>
                        <el-radio :label="0">0分</el-radio>
                      </el-radio-group>
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="内向(I)">
                      <el-radio-group v-model="form.aiscore" @change="val => handleAScoreChange('I', val)">
                        <el-radio :label="1">1分</el-radio>
                        <el-radio :label="0">0分</el-radio>
                      </el-radio-group>
                    </el-form-item>
                  </el-col>
                </template>
                <template v-if="form.dimension === 'SN'">
                  <el-col :span="12">
                    <el-form-item label="感觉(S)">
                      <el-radio-group v-model="form.asscore" @change="val => handleAScoreChange('S', val)">
                        <el-radio :label="1">1分</el-radio>
                        <el-radio :label="0">0分</el-radio>
                      </el-radio-group>
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="直觉(N)">
                      <el-radio-group v-model="form.anscore" @change="val => handleAScoreChange('N', val)">
                        <el-radio :label="1">1分</el-radio>
                        <el-radio :label="0">0分</el-radio>
                      </el-radio-group>
                    </el-form-item>
                  </el-col>
                </template>
                <template v-if="form.dimension === 'TF'">
                  <el-col :span="12">
                    <el-form-item label="思考(T)">
                      <el-radio-group v-model="form.atscore" @change="val => handleAScoreChange('T', val)">
                        <el-radio :label="1">1分</el-radio>
                        <el-radio :label="0">0分</el-radio>
                      </el-radio-group>
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="情感(F)">
                      <el-radio-group v-model="form.afscore" @change="val => handleAScoreChange('F', val)">
                        <el-radio :label="1">1分</el-radio>
                        <el-radio :label="0">0分</el-radio>
                      </el-radio-group>
                    </el-form-item>
                  </el-col>
                </template>
                <template v-if="form.dimension === 'JP'">
                  <el-col :span="12">
                    <el-form-item label="判断(J)">
                      <el-radio-group v-model="form.ajscore" @change="val => handleAScoreChange('J', val)">
                        <el-radio :label="1">1分</el-radio>
                        <el-radio :label="0">0分</el-radio>
                      </el-radio-group>
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="知觉(P)">
                      <el-radio-group v-model="form.apscore" @change="val => handleAScoreChange('P', val)">
                        <el-radio :label="1">1分</el-radio>
                        <el-radio :label="0">0分</el-radio>
                      </el-radio-group>
                    </el-form-item>
                  </el-col>
                </template>
              </el-row>
            </div>

            <div class="score-section">
              <h4>选项B分数</h4>
              <el-row :gutter="20">
                <template v-if="form.dimension === 'EI'">
                  <el-col :span="12">
                    <el-form-item label="外向(E)">
                      <el-radio-group v-model="form.bescore" @change="val => handleBScoreChange('E', val)">
                        <el-radio :label="1">1分</el-radio>
                        <el-radio :label="0">0分</el-radio>
                      </el-radio-group>
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="内向(I)">
                      <el-radio-group v-model="form.biscore" @change="val => handleBScoreChange('I', val)">
                        <el-radio :label="1">1分</el-radio>
                        <el-radio :label="0">0分</el-radio>
                      </el-radio-group>
                    </el-form-item>
                  </el-col>
                </template>
                <template v-if="form.dimension === 'SN'">
                  <el-col :span="12">
                    <el-form-item label="感觉(S)">
                      <el-radio-group v-model="form.bsscore" @change="val => handleBScoreChange('S', val)">
                        <el-radio :label="1">1分</el-radio>
                        <el-radio :label="0">0分</el-radio>
                      </el-radio-group>
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="直觉(N)">
                      <el-radio-group v-model="form.bnscore" @change="val => handleBScoreChange('N', val)">
                        <el-radio :label="1">1分</el-radio>
                        <el-radio :label="0">0分</el-radio>
                      </el-radio-group>
                    </el-form-item>
                  </el-col>
                </template>
                <template v-if="form.dimension === 'TF'">
                  <el-col :span="12">
                    <el-form-item label="思考(T)">
                      <el-radio-group v-model="form.btscore" @change="val => handleBScoreChange('T', val)">
                        <el-radio :label="1">1分</el-radio>
                        <el-radio :label="0">0分</el-radio>
                      </el-radio-group>
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="情感(F)">
                      <el-radio-group v-model="form.bfscore" @change="val => handleBScoreChange('F', val)">
                        <el-radio :label="1">1分</el-radio>
                        <el-radio :label="0">0分</el-radio>
                      </el-radio-group>
                    </el-form-item>
                  </el-col>
                </template>
                <template v-if="form.dimension === 'JP'">
                  <el-col :span="12">
                    <el-form-item label="判断(J)">
                      <el-radio-group v-model="form.bjscore" @change="val => handleBScoreChange('J', val)">
                        <el-radio :label="1">1分</el-radio>
                        <el-radio :label="0">0分</el-radio>
                      </el-radio-group>
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="知觉(P)">
                      <el-radio-group v-model="form.bpscore" @change="val => handleBScoreChange('P', val)">
                        <el-radio :label="1">1分</el-radio>
                        <el-radio :label="0">0分</el-radio>
                      </el-radio-group>
                    </el-form-item>
                  </el-col>
                </template>
              </el-row>
            </div>
          </div>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="submitForm">确 定</el-button>
        </div>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
import { getQuestions, createQuestion, updateQuestion, deleteQuestion, getQuestion } from '../../../api/question'

export default {
  name: 'QuestionList',
  data() {
    return {
      loading: false,
      dialogVisible: false,
      isEdit: false,
      form: {
        id: null,
        content: '',
        dimension: '',
        optionA: '',
        optionB: '',
        aescore: 0,
        aiscore: 0,
        asscore: 0,
        anscore: 0,
        atscore: 0,
        afscore: 0,
        ajscore: 0,
        apscore: 0,
        bescore: 0,
        biscore: 0,
        bsscore: 0,
        bnscore: 0,
        btscore: 0,
        bfscore: 0,
        bjscore: 0,
        bpscore: 0
      },
      questions: [],
      currentPage: 1,
      pageSize: 10,
      total: 0,
      filterForm: {
        content: '',
        dimension: ''
      },
      dialogTitle: '添加问题',
      rules: {
        content: [
          { required: true, message: '请输入问题内容', trigger: 'blur' },
          { max: 500, message: '问题内容长度不能超过500个字符', trigger: 'blur' }
        ],
        dimension: [
          { required: true, message: '请选择维度', trigger: 'change' }
        ],
        optionA: [
          { required: true, message: '请输入选项A', trigger: 'blur' },
          { max: 200, message: '选项A长度不能超过200个字符', trigger: 'blur' }
        ],
        optionB: [
          { required: true, message: '请输入选项B', trigger: 'blur' },
          { max: 200, message: '选项B长度不能超过200个字符', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.fetchQuestions()
  },
  methods: {
    // 获取维度标签类型
    getDimensionTagType(dimension) {
      const types = {
        'EI': 'primary',
        'SN': 'success',
        'TF': 'warning',
        'JP': 'danger'
      }
      return types[dimension] || 'info'
    },

    // 获取维度标签文本
    getDimensionLabel(dimension) {
      const labels = {
        'EI': '外向-内向',
        'SN': '感觉-直觉',
        'TF': '思考-情感',
        'JP': '判断-知觉'
      }
      return labels[dimension] || dimension
    },

    // 重置所有分数
    resetScores() {
      const scoreFields = [
        'aescore', 'aiscore', 'asscore', 'anscore', 'atscore', 'afscore', 'ajscore', 'apscore',
        'bescore', 'biscore', 'bsscore', 'bnscore', 'btscore', 'bfscore', 'bjscore', 'bpscore'
      ]
      scoreFields.forEach(field => {
        this.form[field] = 0
      })
    },

    // 监听维度变化
    handleDimensionChange(dimension) {
      // 重置所有分数
      this.resetScores()
      // 根据维度自动设置默认分数
      this.setDefaultScores(dimension)
    },

    // 设置默认分数
    setDefaultScores(dimension) {
      // 重置所有分数，确保之前的选项不会影响新的选择
      this.resetScores();
      
      // 默认情况下，两个都是不选中状态
      // 我们只是显示选项，但不自动选中
      // 用户需要手动点击选择想要的分值
    },

    // 处理A选项分数变化
    handleAScoreChange(dimension, value) {
      if (value === 1) {
        // 重置当前维度所有分数
        this.resetDimensionScores(dimension)
        // 设置对应的分数
        switch (dimension) {
          case 'E':
            this.form.aescore = 1
            this.form.aiscore = 0
            // 设置B选项的相反值
            this.form.bescore = 0
            this.form.biscore = 1
            break
          case 'I':
            this.form.aiscore = 1
            this.form.aescore = 0
            // 设置B选项的相反值
            this.form.biscore = 0
            this.form.bescore = 1
            break
          case 'S':
            this.form.asscore = 1
            this.form.anscore = 0
            // 设置B选项的相反值
            this.form.bsscore = 0
            this.form.bnscore = 1
            break
          case 'N':
            this.form.anscore = 1
            this.form.asscore = 0
            // 设置B选项的相反值
            this.form.bnscore = 0
            this.form.bsscore = 1
            break
          case 'T':
            this.form.atscore = 1
            this.form.afscore = 0
            // 设置B选项的相反值
            this.form.btscore = 0
            this.form.bfscore = 1
            break
          case 'F':
            this.form.afscore = 1
            this.form.atscore = 0
            // 设置B选项的相反值
            this.form.bfscore = 0
            this.form.btscore = 1
            break
          case 'J':
            this.form.ajscore = 1
            this.form.apscore = 0
            // 设置B选项的相反值
            this.form.bjscore = 0
            this.form.bpscore = 1
            break
          case 'P':
            this.form.apscore = 1
            this.form.ajscore = 0
            // 设置B选项的相反值
            this.form.bpscore = 0
            this.form.bjscore = 1
            break
        }
      }
    },

    // 重置指定维度的所有分数
    resetDimensionScores(dimension) {
      switch (dimension) {
        case 'E':
        case 'I':
          this.form.aescore = 0
          this.form.aiscore = 0
          this.form.bescore = 0
          this.form.biscore = 0
          break
        case 'S':
        case 'N':
          this.form.asscore = 0
          this.form.anscore = 0
          this.form.bsscore = 0
          this.form.bnscore = 0
          break
        case 'T':
        case 'F':
          this.form.atscore = 0
          this.form.afscore = 0
          this.form.btscore = 0
          this.form.bfscore = 0
          break
        case 'J':
        case 'P':
          this.form.ajscore = 0
          this.form.apscore = 0
          this.form.bjscore = 0
          this.form.bpscore = 0
          break
      }
    },

    // 处理对话框关闭
    handleDialogClose() {
      if (this.$refs.form) {
        this.$refs.form.resetFields()
      }
      // 重置表单数据
      this.form = {
        id: null,
        content: '',
        dimension: '',
        optionA: '',
        optionB: '',
        aescore: 0,
        aiscore: 0,
        asscore: 0,
        anscore: 0,
        atscore: 0,
        afscore: 0,
        ajscore: 0,
        apscore: 0,
        bescore: 0,
        biscore: 0,
        bsscore: 0,
        bnscore: 0,
        btscore: 0,
        bfscore: 0,
        bjscore: 0,
        bpscore: 0
      }
      this.isEdit = false
      this.dialogTitle = '添加问题'
    },

    // 格式化分数显示
    formatScore(row, score) {
      const value = row[score]
      return value !== null && value !== undefined ? Number(value) : 0
    },

    // 获取问题列表
    async fetchQuestions() {
      try {
        this.loading = true
        const params = {
          page: this.currentPage,
          size: this.pageSize,
          content: this.filterForm.content,
          dimension: this.filterForm.dimension
        }
        const res = await getQuestions(params)
        if (res.code === 200) {
          // 确保所有分数字段都是数字类型
          this.questions = res.data.list.map(item => ({
            ...item,
            aescore: this.formatScore(item, 'aescore'),
            aiscore: this.formatScore(item, 'aiscore'),
            asscore: this.formatScore(item, 'asscore'),
            anscore: this.formatScore(item, 'anscore'),
            atscore: this.formatScore(item, 'atscore'),
            afscore: this.formatScore(item, 'afscore'),
            ajscore: this.formatScore(item, 'ajscore'),
            apscore: this.formatScore(item, 'apscore'),
            bescore: this.formatScore(item, 'bescore'),
            biscore: this.formatScore(item, 'biscore'),
            bsscore: this.formatScore(item, 'bsscore'),
            bnscore: this.formatScore(item, 'bnscore'),
            btscore: this.formatScore(item, 'btscore'),
            bfscore: this.formatScore(item, 'bfscore'),
            bjscore: this.formatScore(item, 'bjscore'),
            bpscore: this.formatScore(item, 'bpscore')
          }))
          this.total = res.data.total
        } else {
          this.$message.error(res.msg || '获取问题列表失败')
        }
      } catch (error) {
        console.error('获取问题列表失败:', error)
        this.$message.error('获取问题列表失败')
      } finally {
        this.loading = false
      }
    },

    // 处理编辑
    async handleEdit(row) {
      try {
        this.loading = true;
        console.log('编辑行数据:', row); // 调试用
        
        // 使用API获取最新问题数据
        const res = await getQuestion(row.id);
        if (res.code === 200) {
          const questionData = res.data;
          console.log('API返回数据:', questionData); // 调试用
          
          // 直接使用小写字段名，这些字段名已在API响应中标准化，并确保所有分数字段为数字类型
          const formData = {
            id: Number(questionData.id),
            content: questionData.content,
            dimension: questionData.dimension,
            optionA: questionData.optionA || '',
            optionB: questionData.optionB || '',
            // 明确强制转换为数字1或0
            aescore: questionData.aescore == 1 ? 1 : 0,
            aiscore: questionData.aiscore == 1 ? 1 : 0,
            asscore: questionData.asscore == 1 ? 1 : 0,
            anscore: questionData.anscore == 1 ? 1 : 0,
            atscore: questionData.atscore == 1 ? 1 : 0,
            afscore: questionData.afscore == 1 ? 1 : 0,
            ajscore: questionData.ajscore == 1 ? 1 : 0,
            apscore: questionData.apscore == 1 ? 1 : 0,
            bescore: questionData.bescore == 1 ? 1 : 0,
            biscore: questionData.biscore == 1 ? 1 : 0,
            bsscore: questionData.bsscore == 1 ? 1 : 0,
            bnscore: questionData.bnscore == 1 ? 1 : 0,
            btscore: questionData.btscore == 1 ? 1 : 0,
            bfscore: questionData.bfscore == 1 ? 1 : 0,
            bjscore: questionData.bjscore == 1 ? 1 : 0,
            bpscore: questionData.bpscore == 1 ? 1 : 0
          };
          
          console.log('处理后的表单数据:', formData); // 调试用
          
          this.isEdit = true;
          this.dialogTitle = '编辑问题';
          this.form = formData;
          this.dialogVisible = true;
        } else {
          this.$message.error(res.msg || '获取问题详情失败');
        }
      } catch (error) {
        console.error('处理编辑数据失败:', error);
        this.$message.error('处理编辑数据失败');
      } finally {
        this.loading = false;
      }
    },

    // 提交表单
    submitForm() {
      this.$refs.form.validate(async (valid) => {
        if (valid) {
          try {
            this.loading = true
            // 构建一个新的formData对象，使用驼峰式字段名
            const formData = {
              ...this.form,
              // 使用驼峰命名字段，与后端QuestionUpdateDTO.java中的@JsonProperty注解匹配
              aEScore: Number(this.form.aescore),
              aIScore: Number(this.form.aiscore),
              aSScore: Number(this.form.asscore),
              aNScore: Number(this.form.anscore),
              aTScore: Number(this.form.atscore),
              aFScore: Number(this.form.afscore),
              aJScore: Number(this.form.ajscore),
              aPScore: Number(this.form.apscore),
              bEScore: Number(this.form.bescore),
              bIScore: Number(this.form.biscore),
              bSScore: Number(this.form.bsscore),
              bNScore: Number(this.form.bnscore),
              bTScore: Number(this.form.btscore),
              bFScore: Number(this.form.bfscore),
              bJScore: Number(this.form.bjscore),
              bPScore: Number(this.form.bpscore),
              // 保留基本信息字段
              content: this.form.content,
              dimension: this.form.dimension,
              optionA: this.form.optionA,
              optionB: this.form.optionB
            }
            
            // 添加调试日志
            console.log('准备发送的更新数据:', formData);

            let res
            if (this.isEdit) {
              res = await updateQuestion(this.form.id, formData)
            } else {
              res = await createQuestion(formData)
            }

            if (res.code === 200) {
              this.$message.success(this.isEdit ? '更新成功' : '创建成功')
              this.dialogVisible = false
              // 重置表单
              this.resetForm()
              // 强制刷新数据
              await this.forceRefresh()
            } else {
              this.$message.error(res.msg || (this.isEdit ? '更新失败' : '创建失败'))
            }
          } catch (error) {
            console.error('提交失败:', error)
            this.$message.error('操作失败：' + (error.message || '未知错误'))
          } finally {
            this.loading = false
          }
        }
      })
    },

    // 强制刷新数据
    async forceRefresh() {
      // 清空当前数据
      this.questions = []
      // 重新获取数据
      await this.fetchQuestions()
    },

    // 重置表单
    resetForm() {
      if (this.$refs.form) {
        this.$refs.form.resetFields()
      }
      this.form = {
        id: null,
        content: '',
        dimension: '',
        optionA: '',
        optionB: '',
        aescore: 0,
        aiscore: 0,
        asscore: 0,
        anscore: 0,
        atscore: 0,
        afscore: 0,
        ajscore: 0,
        apscore: 0,
        bescore: 0,
        biscore: 0,
        bsscore: 0,
        bnscore: 0,
        btscore: 0,
        bfscore: 0,
        bjscore: 0,
        bpscore: 0
      }
    },

    // 处理删除
    async handleDelete(row) {
      try {
        await this.$confirm('确认删除该问题吗？', '提示', {
          type: 'warning'
        })
        
        const res = await deleteQuestion(row.id)
        if (res.code === 200) {
          this.$message.success('删除成功')
          // 如果当前页只有一条数据，且不是第一页，则跳转到上一页
          if (this.questions.length === 1 && this.currentPage > 1) {
            this.currentPage--
          }
          await this.forceRefresh()
        } else {
          this.$message.error(res.msg || '删除失败')
        }
      } catch (error) {
        if (error !== 'cancel') {
          console.error('删除失败:', error)
          this.$message.error('删除失败：' + (error.message || '未知错误'))
        }
      }
    },

    // 处理筛选
    handleFilter() {
      this.currentPage = 1
      this.forceRefresh()
    },

    // 重置筛选
    resetFilter() {
      this.filterForm = {
        content: '',
        dimension: ''
      }
      this.handleFilter()
    },

    // 处理页码变化
    handleCurrentChange(page) {
      this.currentPage = page
      this.forceRefresh()
    },

    // 处理每页条数变化
    handleSizeChange(size) {
      this.pageSize = size
      this.currentPage = 1
      this.forceRefresh()
    },

    // 处理添加
    handleAdd() {
      this.isEdit = false
      this.dialogTitle = '添加问题'
      this.dialogVisible = true
      this.form = {
        id: null,
        content: '',
        dimension: '',
        optionA: '',
        optionB: '',
        aescore: 0,
        aiscore: 0,
        asscore: 0,
        anscore: 0,
        atscore: 0,
        afscore: 0,
        ajscore: 0,
        apscore: 0,
        bescore: 0,
        biscore: 0,
        bsscore: 0,
        bnscore: 0,
        btscore: 0,
        bfscore: 0,
        bjscore: 0,
        bpscore: 0
      }
    },

    // 处理B选项分数变化
    handleBScoreChange(dimension, value) {
      if (value === 1) {
        // 重置当前维度所有分数
        this.resetDimensionScores(dimension)
        // 设置对应的分数
        switch (dimension) {
          case 'E':
            this.form.bescore = 1
            this.form.biscore = 0
            // 设置A选项的相反值
            this.form.aescore = 0
            this.form.aiscore = 1
            break
          case 'I':
            this.form.biscore = 1
            this.form.bescore = 0
            // 设置A选项的相反值
            this.form.aiscore = 0
            this.form.aescore = 1
            break
          case 'S':
            this.form.bsscore = 1
            this.form.bnscore = 0
            // 设置A选项的相反值
            this.form.asscore = 0
            this.form.anscore = 1
            break
          case 'N':
            this.form.bnscore = 1
            this.form.bsscore = 0
            // 设置A选项的相反值
            this.form.anscore = 0
            this.form.asscore = 1
            break
          case 'T':
            this.form.btscore = 1
            this.form.bfscore = 0
            // 设置A选项的相反值
            this.form.atscore = 0
            this.form.afscore = 1
            break
          case 'F':
            this.form.bfscore = 1
            this.form.btscore = 0
            // 设置A选项的相反值
            this.form.afscore = 0
            this.form.atscore = 1
            break
          case 'J':
            this.form.bjscore = 1
            this.form.bpscore = 0
            // 设置A选项的相反值
            this.form.ajscore = 0
            this.form.apscore = 1
            break
          case 'P':
            this.form.bpscore = 1
            this.form.bjscore = 0
            // 设置A选项的相反值
            this.form.apscore = 0
            this.form.ajscore = 1
            break
        }
      }
    }
  },
  watch: {
    'form.dimension': {
      handler: 'handleDimensionChange',
      immediate: true
    }
  }
}
</script>

<style scoped>
.question-container {
  padding: 20px;
}

.question-card {
  margin-bottom: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.filter-container {
  margin-bottom: 20px;
}

.pagination-container {
  margin-top: 20px;
  text-align: right;
}

.score-settings {
  margin-top: 20px;
  padding: 20px;
  border: 1px solid #ebeef5;
  border-radius: 4px;
}

.score-tip {
  color: #666;
  font-size: 14px;
  margin-bottom: 20px;
}

.score-section {
  margin-bottom: 20px;
}

.score-section h4 {
  margin-bottom: 15px;
  color: #333;
}

.score-info {
  margin-top: 5px;
}

.score-info .el-tag {
  margin-right: 5px;
}
</style> 